<template>
  <div id="testlist">
    <el-dialog title="新增IP" :visible.sync="ipDetailVisible" width="500px" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="editForm" label-width="200px" ref="editForm" :inline="true" style="padding:10px 0 0;clear:both" :rules="editFormRules">
        <el-row>
          <el-col>
            <table cellspacing="0" cellpadding="0" class="mytable">
              <tr>
                <td>
                  <el-form-item prop="ip">
                    <el-input v-model.trim="editForm.ip" style="width:300px" placeholder="请输入IP"></el-input>
                    <el-button type="success" @click.native="editFormSubmit()" style="margin-left:20px;">确定</el-button>
                  </el-form-item>
                </td>
              </tr>
            </table>
            <template>
            </template>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item>人事司IP白名单维护</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs" style="height:90px">
    <!--工具条-->
        <el-form :inline="true">
          <div>
            <div style="color:red;font-weight: bolder;">说明：下表中IP地址为人事司IP白名单。只有在IP白名单内的计算机才能用人事司账号登录，非人事司账号不受此白名单限制。</div>
            <el-form-item>
              <el-button type="success" @click="addnew" icon="el-icon-plus" style="margin-top:10px">新增</el-button>
            </el-form-item>
          </div>
        </el-form>
    </section>
    <section class="wboxd" style="margin-top:10px">
      <!--列表-->
      <h1>通知列表</h1>
      <el-table :data="ipList" highlight-current-row v-loading="listLoading" style="width: 100%;" :height="tableHeight" border>
        <el-table-column type="index" label=" " width="50" fixed align="center" class-name="tableth">
        </el-table-column>
        <el-table-column prop="ip" label="IP" width="300">
          <template slot-scope="scope">
            {{ scope.row }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" @click="delIp(scope.$index)" type="text" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </section>
  </div>
</template>

<script>
export default {
  data () {
    return {
      editForm: {
        ip: ''
      },
      editFormRules: {
        ip: [
          { required: true, message: '必填项', trigger: 'change' }
        ]
      },
      title: '',
      submitLoading: false,
      ipDetailVisible: false,
      ipList: [],
      listLoading: false,
      tableHeight: 100
    }
  },
  methods: {
    editFormSubmit () {
      console.log('已点击')
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          if (this.ipList.includes(this.editForm.ip)) {
            this.$message({
              message: '该IP已存在',
              type: 'error'
            })
          } else {
            this.ipList.push(this.editForm.ip)
            this.save()
            this.ipDetailVisible = false
          }
        }
      })
    },
    delIp (index) {
      this.$confirm('确定要删除该IP吗?', '提示', {
        type: 'success'
      }).then(() => {
        console.log(index)
        this.ipList.splice(index, 1)
        this.save()
      })
    },
    addnew () {
      this.ipDetailVisible = true
      this.editForm.ip = ''
    },
    getList () {
      this.listLoading = true
      this.$http
        .get(`/configip/getips`)
        .then(response => {
          this.listLoading = false
          this.ipList = response.data
        })
        .catch(error => {
          this.listLoading = false
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
    },
    save () {
      this.listLoading = true
      this.$http
        .post(`/configip/saveips`, this.ipList)
        .then(response => {
          this.listLoading = false
          if (response.data.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '操作失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.listLoading = false
          console.log(error)
          this.$message({
            message: '网络操作失败:' + error,
            type: 'error'
          })
        })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - 302
      })
    }
  },
  mounted () {
    this.getList()
    this.tableresize(this)
    let _this = this
    window.onresize = function windowResize () {
      // 通过捕获系统的onresize事件触发我们需要执行的事件
      _this.tableresize(_this)
    }
  }
}
</script>
<style>
  .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .wboxd .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  .el-table--border th, .el-table--border td {
    border-right: 1px solid #95B8E7;
  }
  .el-table th.is-leaf, .el-table td {
    border-bottom: 1px solid #95B8E7;
  }

</style>
<style scoped>
  h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  .wboxs{
    padding:5px 15px;
    width: calc(100% - 30px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
  .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
